﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AttributesFramework.Mapper
{
    public abstract class Util
    {
        public static bool isWebService()
        {
            return System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request.RawUrl.Contains("asmx");
        }

        public static void clearMappedTypes()
        {
            Statics.MappedTypes = new List<Type>();
        }

        public static bool setMappedType(Type p_Type)
        {
            if (Statics.MappedTypes.Contains(p_Type))
                return true;

            Statics.MappedTypes.Add(p_Type);
            return false;
        }

        public static bool ExistMap(Type p_ClassType)
        {
            AttributesFramework.Attributes.DBTableAttribute _classTable = Attributes.GetDBTableAttribute(p_ClassType);
            return ExistTable(_classTable.Name);
        }

        public static bool ExistMap(Type p_ClassType, SqlTransaction p_Transaction)
        {
            AttributesFramework.Attributes.DBTableAttribute _classTable = Attributes.GetDBTableAttribute(p_ClassType);
            return ExistTable(_classTable.Name, p_Transaction);
        }

        public static bool ExistTable(string p_classTable)
        {
            return (int)AttributesFramework.Data.Provider.ExecuteScalar(CommandType.Text, "SELECT COUNT(*) FROM SYSOBJECTS WHERE XTYPE = 'U' AND NAME = '" + p_classTable + "'", null) > 0;
        }

        public static bool ExistTable(string p_classTable, SqlTransaction p_Transaction)
        {
            return (int)AttributesFramework.Data.Provider.ExecuteScalar(p_Transaction, CommandType.Text, "SELECT COUNT(*) FROM SYSOBJECTS WHERE XTYPE = 'U' AND NAME = '" + p_classTable + "'", null) > 0;
        }

        public static string GetSQLValue(object p_Object, AttributesFramework.Attributes.DBColumnAttribute p_Atribute)
        {
            object _value = p_Object.GetType().GetProperty(p_Atribute.PropertyName).GetValue(p_Object, null);

            if (_value == null)
                return "NULL";

            switch (p_Atribute.DataType)
            {
                case SqlDbType.VarChar:
                    return AttributesFramework.Data.Util.toVarchar(Convert.ToString(_value));
                case SqlDbType.Text:
                    return AttributesFramework.Data.Util.toVarchar(Convert.ToString(_value));
                case SqlDbType.DateTime:
                    return AttributesFramework.Data.Util.toDateTime(Convert.ToDateTime(_value));
                case SqlDbType.UniqueIdentifier:
                    return AttributesFramework.Data.Util.toGUID((Guid)_value);
                case SqlDbType.Int:
                    return AttributesFramework.Data.Util.toInt(Convert.ToInt32(_value));
                case SqlDbType.BigInt:
                    return AttributesFramework.Data.Util.toInt(Convert.ToInt64(_value));
                case SqlDbType.Decimal:
                    return AttributesFramework.Data.Util.toDecimal(Convert.ToDecimal(_value));
                case SqlDbType.Float:
                    return AttributesFramework.Data.Util.toDouble(Convert.ToDouble(_value));
                case SqlDbType.Bit:
                    return AttributesFramework.Data.Util.toBit(Convert.ToBoolean(_value));
                default:
                    break;
            }

            return "NULL";
        }

        public static string GetEmptyValue(SqlDbType p_DataType)
        {
            if (p_DataType == SqlDbType.Int)
            {
                return AttributesFramework.Data.Util.toInt(0);
            }
            else if (p_DataType == SqlDbType.BigInt)
            {
                return AttributesFramework.Data.Util.toInt(0);
            }
            else if (p_DataType == SqlDbType.UniqueIdentifier)
            {
                return AttributesFramework.Data.Util.toGUID(Guid.Empty);
            }
            else if (p_DataType == SqlDbType.Float)
            {
                return AttributesFramework.Data.Util.toFloat(0);
            }
            else if (p_DataType == SqlDbType.VarChar)
            {
                return AttributesFramework.Data.Util.toVarchar("");
            }
            else if (p_DataType == SqlDbType.Text)
            {
                return AttributesFramework.Data.Util.toVarchar("");
            }

            return null;
        }
    }
}
